const model = require('../model/newsModel')
const moment = require('moment')

class NewsService {

  pageSize = 2

  async index(page = 1) {
    let skip = (page - 1) * this.pageSize
    let data = await model.all({}, { updatedAt: -1 }, skip, this.pageSize)

    data.map(item => {
      item.id = item._id
      item.at = moment(item.updatedAt).format('YYYY-MM-DD HH:mm:ss')
      return item
    })
    return data
  }

  async size() {
    let total = await model.count({})
    let pageLimit = Math.ceil(total / this.pageSize)
    return pageLimit
  }

  async create(data) {
    let rows = await model.create(data);
    return rows;
  }

  async getIdToInfo(id) {
    return model.byId(id)
  }

  async update(id, data) {
    return model.update({ _id: id }, data)
  }

  async destory(id) {
    return await model.update({ _id: id }, { isdel: true })
  }

}

module.exports = new NewsService()